<template>
  <div class="finish-com">
    <div ref="maychar" style="width:300px;height:500px;background-color: rgba(0, 0, 0, 0);">
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  data() {
    return {
      chartData: []
    }
  },
  mounted() {
    this.fetchChartData();
  },
  methods: {
    fetchChartData() {
      this.$axios.get('/api/index/touristStatistics')
        .then(response => {
          const data = response.data.data;
          this.chartData = data.outside_country.map(item => ({
            name: item.name,
            value: parseInt(item.value)
          }));
          this.getCharts();
        })
        .catch(error => {
          console.error('Error fetching data:', error);
        });
    },
    getCharts() {
      const chartBox = echarts.init(this.$refs.maychar);
      const option = {
        title: {
          text: '国外游客来源地',
          left: 'center',
          top: '10%',
          textStyle: {
            fontSize: 24,
            color: "#fff"
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)',
          textStyle: {
            fontSize:'20'
          }
        },
        legend: {
          top: 'bottom',
          textStyle: {
            color: '#fff',
            fontSize:'20'
          }
        },
        series: [
          {
            name: '来源地',
            type: 'pie',
            radius: [10, 130],
            center: ['50%', '50%'],
            roseType: 'area',
            itemStyle: {
              borderRadius: 1
            },
            label: {
              show:false,
            },
            labelLine: {
              show:false,
              lineStyle: {
                color: '#fff'
              }
            },
            data: this.chartData
          }
        ]
      };
      chartBox.setOption(option);
      // Adjust chart size based on window size
      window.addEventListener("resize", function () {
        chartBox.resize();
      });
    }
  }
}
</script>

<style lang="scss" scoped></style>
